package com.kasax.hinetwork;

import com.kasax.hinetwork.processor.HttpClientProcessorImpl;
import com.kasax.hinetwork.processor.HttpUrlConnectionProcessorImpl;
import com.kasax.hinetwork.processor.OkHttpClientProcessorImpl;
import com.kasax.hinetwork.processor.RetrofitProcessorImpl;

public class HttpApiManager {

    private IHttpProcessor processor;
    private StrategyConfig appStrategyConfig;

    private HttpApiManager() {
    }

    public static HttpApiManager getInstance() {
        return Holder.INSTANCE;
    }

    public void init(StrategyConfig config) {
        this.appStrategyConfig = config;
        switch (appStrategyConfig.getCoreType()) {
            case OKHTTP:
                processor = new OkHttpClientProcessorImpl(appStrategyConfig);
                break;
            case RETROFIT:
                processor = new RetrofitProcessorImpl(appStrategyConfig);
                break;
            case HTTP_CLIENT:
                processor = new HttpClientProcessorImpl(appStrategyConfig);
                break;
            default:
                processor = new HttpUrlConnectionProcessorImpl(appStrategyConfig);
                break;
        }
    }

    public <T> HttpResponse<T> get(HttpRequest request) {
        return processor.get(request);
    }

    public <T> void getAsync(HttpRequest httpRequest, AsyncCallback<HttpResponse<T>> callback) {
        processor.getAsync(httpRequest, callback);
    }

    public <T> HttpResponse<T> post(HttpRequest request) {
        return processor.post(request);
    }

    public <T> void postAsync(HttpRequest httpRequest, AsyncCallback<HttpResponse<T>> callback) {
        processor.postAsync(httpRequest, callback);
    }

    private static class Holder {

        public static final HttpApiManager INSTANCE = new HttpApiManager();
    }
}
